#include <cstdio>
#include <cstdlib>
#include <math.h>
#include "vectclasses.hpp"


Vector::Vector(int length) : myLength(length), myDataCnt(0)
{
    myData = new double[myLength];
}


Vector::~Vector()
{
    myLength = 0;
    delete [] myData;
}

Vector::Vector(Vector const &a) : myLength(a.myLength),myDataCnt(0)
{
    myData = new double[myLength];

    for (int i = 0; i < myLength; i++)
    {
        myData[i] = a.myData[i];
    }
}

Vector & Vector::operator = (Vector const &a)
{
    if (this == &a)
    {
        return *this;
    }

    if (myData != NULL)
    {
        delete [] myData;
    }
    myLength = a.myLength;
    myData = new double[myLength];
    for (int i = 0; i < myLength; i++)
    {
        myData[i] = a.myData[i];
    }
    return *this;
}

int Vector::getLength() const
{
    return myLength;
}

void Vector::normalize()
{
    double sum = 0;
    for (int i = 0; i < myLength; i++)
    {
        sum += myData[i]*myData[i];
    }
    for (int i = 0; i < myLength; i++)
    {
        myData[i] /= sqrt(sum);
    }
}

double Vector::multiply(Vector a)
{
    double result = 0;
    if (myLength != a.myLength)
    {
        printf("You can't multiply vectors if their lengths are different.\n");
        return -1;
    }
    for (int i = 0; i < myLength; i++)
    {
        result += myData[i]*a.myData[i];
    }
    return result;
}

void Vector::add(Vector a)
{
    if (myLength != a.myLength)
    {
        printf("You can't add vectors if their lengths are different.\n");
        return;
    }
    for (int i = 0; i < myLength; i++)
    {
        myData[i] += a.myData[i];
    }
}

double & Vector::at(int i)
{
    return myData[i];
}

int Vector::maxPlace() const
{
	double tmp = myData[0];
	int place = 0;
	for (int i = 0; i < myLength; i++)
	{
		if (myData[i] > tmp)
		{
			tmp = myData[i];
			place = i;
		}
	}
	return place;
}


VectorArray::VectorArray(int number, int length) : myVectNumber(number), myVectLength(length)
{
    myVData = (Vector**)malloc(number*sizeof(Vector*));
    //myData = new Vector*[number];
    for (int i = 0; i < number; i++)
    {
            myVData[i] = new Vector(length);
    }
}

VectorArray::~VectorArray()
{
    if (myVData != NULL)
    {
        for (int i = 0; i < myVectNumber; i++)
        {
            delete myVData[i];
        }
        free(myVData);
    }
}

VectorArray & VectorArray::operator = (VectorArray const &a)
{
    if (this == &a)
    {
        return *this;
    }
    if (myVData != NULL)
    {
        for (int i = 0; i < myVectNumber; i++)
        {
            delete myVData[i];
        }
        free(myVData);
    }
    myVectLength = a.myVectLength;
    myVectNumber = a.myVectNumber;
    myVData = (Vector**)malloc(myVectNumber*sizeof(Vector*));
    for (int i = 0; i < myVectNumber; i++)
    {
        *(myVData[i]) = *(a.myVData[i]);
    }
    return *this;
}

VectorArray::VectorArray(VectorArray const &a) : myVectNumber(a.myVectNumber), myVectLength(a.myVectLength)
{
    myVData = (Vector**)malloc(myVectNumber*sizeof(Vector*));
    for (int i = 0; i < myVectNumber; i++)
    {
        *(myVData[i]) = *(a.myVData[i]);
    }
}

int VectorArray::getVectLength() const
{
    return myVectLength;
}

int VectorArray::getVectNumber() const
{
    return myVectNumber;
}

Vector & VectorArray::getVector(int i)
{
    return *(myVData[i]);
}

double & VectorArray::at(int i, int j)
{
    return (myVData[i])->at(j);
}

void VectorArray::read(FILE * f)
{
    for (int i = 0; i < myVectNumber; i++)
    {
        for (int j = 0; j < myVectLength; j++)
        {
            fscanf(f, "%lf", &((myVData[i])->at(j)));
        }
    }
}

